<?php
/**
 * Created by 1971599474@qq.com
 * User: zhixuan
 * Date: 2020/1/17
 * Time: 14:15
 */

namespace app\index\controller;
use app\common\model\Discount as DiscountModel;

class Discount extends Base
{

    public function index(){
        $params = array_filter(request()->param());
        $page = ! empty($params['page']) && $params['page'] >= 1 ? $params['page'] : 1;
        $time = time();
        $where[] = ['start_time','<',$time];
        $where[] = ['end_time','>',$time];
        if($discount_id = isset($params['discount_id'])){
            $where[] = ['discount_id','=',$params['discount_id']];
        }
        if($discount_name = isset($params['discount_name'])){
            $where[] = ['discount_name','like',"%{$params['discount_name']}%"];
        }
        if($shop_id = isset($params['shop_id'])){
            $where[] = ['shop_id','like',"%{$params['shop_id']}%"];
        }
        if($goods_id = isset($params['goods_id'])){
            $where[] = ['goods_id','like',"%{$params['goods_id']}%"];
        }
        $condition = [
            'where' => $where,
            'with'  => ['getGoods'],
            'order' => ['discount_id' => 'desc'],
            'paginate' => [
                'page' => $page,
                'query' => $params,
                'list_rows'=>20,
            ],
        ];
        $discount = DiscountModel::getPageAll($condition);

        return $this->fetch('index',['discount'=>$discount]);
    }
}